﻿
Partial Public Class AssignMenuPopup
    Inherits System.Web.UI.Page
    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not (Session("Role_no") Is Nothing) Then
                Dim clsGet As New ClassFunctionGetData
                gridAssign.DataSource = clsGet.LoadMenu(Session("Role_no").ToString(), "'Booking','Master','Report','Admin','Operation','ePayment'")
                gridAssign.DataBind()
            End If
            If Session("EditMaster") Is Nothing Then
                btnSave.Visible = False
            End If
        End If
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
        Dim Show As String = ""
        Dim Edit As String = ""
        Dim Add As String = ""
        Dim Delete As String = ""
        Dim Approve As String = ""
        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim tran As System.Data.SqlClient.SqlTransaction
        Application.Lock()
        conn.Open()
        tran = clsDB.BeginTran(conn)
        If (clsFc.DeleteMENU_Role(Session("Role_no").ToString(), conn, tran) > -1) Then
            For i As Integer = 0 To gridAssign.Rows.Count - 1
                Dim chk As CheckBox = CType(gridAssign.Rows(i).FindControl("chkShow"), CheckBox)
                Dim chkList As CheckBoxList = CType(gridAssign.Rows(i).FindControl("chkRole"), CheckBoxList)
                If (chk.Checked) Then
                    Show = "Y"
                Else
                    Show = "N"
                End If
                If (Show = "Y") Then
                    For Each lt As ListItem In chkList.Items
                        If lt.Selected Then
                            Select Case lt.Text
                                Case "Edit" : Edit = "Y"
                                Case "Add" : Add = "Y"
                                Case "Delete" : Delete = "Y"
                                Case "Approve" : Approve = "Y"
                            End Select
                        Else
                            Select Case lt.Text
                                Case "Edit" : Edit = "N"
                                Case "Add" : Add = "N"
                                Case "Delete" : Delete = "N"
                                Case "Approve" : Approve = "N"
                            End Select
                        End If
                    Next
                Else
                    Edit = "N"
                    Add = "N"
                    Delete = "N"
                    Approve = "N"
                End If

                'ทำการ Insert
                If (clsFc.InsertMENU_Role(gridAssign.DataKeys(i).Values("Menu_no"), Session("Role_no").ToString(), Show, Edit, Add, Delete, Approve, conn, tran) < 1) Then
                    condition = False
                End If
            Next
        Else
            condition = False
        End If

        If (condition) Then
            clsDB.CommitTran(tran)
            Session("Role_no") = Nothing
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModal();alert('บันทึกข้อมูลสำเร็จ');", True)
        Else
            clsDB.RollBackTran(tran)
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('เกิดความผิดพลาดในการบันทึก !!');", True)
        End If
        conn.Close()
        Application.UnLock()
        conn.Dispose()
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCancel.Click
        Session("Role_no") = Nothing
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModal();", True)
    End Sub

    Private Sub gridAssign_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridAssign.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim chk As CheckBox = CType(e.Row.FindControl("chkShow"), CheckBox)
            Dim chkList As CheckBoxList = CType(e.Row.FindControl("chkRole"), CheckBoxList)
            If (gridAssign.DataKeys(e.Row.RowIndex).Values("Show_Menu") & "" = "Y") Then
                chk.Checked = True
                chkList.Visible = True
                If (gridAssign.DataKeys(e.Row.RowIndex).Values("Edit_Menu") & "" = "Y") Then
                    chkList.Items(0).Selected = True
                End If
                If (gridAssign.DataKeys(e.Row.RowIndex).Values("Add_Menu") & "" = "Y") Then
                    chkList.Items(1).Selected = True
                End If
                If (gridAssign.DataKeys(e.Row.RowIndex).Values("Delete_Menu") & "" = "Y") Then
                    chkList.Items(2).Selected = True
                End If
                'If (gridAssign.DataKeys(e.Row.RowIndex).Values("Approve_Menu") & "" = "Y") Then
                '    chkList.Items(3).Selected = True
                'End If
            End If
            If (gridAssign.DataKeys(e.Row.RowIndex).Values("Mode") & "" = "Master") Or (gridAssign.DataKeys(e.Row.RowIndex).Values("Mode") & "" = "Admin") Then
                chkList.Visible = True
            Else
                chkList.Visible = False
            End If
        End If
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "")
        End If
    End Sub
End Class